package com.example.agriculturemachine.ui.activity;

import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;

import com.example.agriculturemachine.R;
import com.example.agriculturemachine.base.BaseActivity;
import com.example.agriculturemachine.untils.StatusBarUtils;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AnalysisActivity extends BaseActivity {

    @BindView(R.id.line_chart)
    public LineChart mLineChart;

    @BindView(R.id.back)
    public ImageView back;
    //x轴样式
    private String[] mLableXHeartRate = new String[]{"一号","二号","三号","四号","五号","六号","七号"};
    @Override
    protected int getLayoutResId() {
        return R.layout.data_analysis;
    }

    @Override
    protected void initData() {

        //设置点的坐标
        List<Entry> entries=new ArrayList<>();
        entries.add(new Entry(0,6));
        entries.add(new Entry(1,12.5f));
        entries.add(new Entry(2,3));
        entries.add(new Entry(3,4));
        entries.add(new Entry(4,8));
        entries.add(new Entry(5,20));
        entries.add(new Entry(6,30));
        List<Entry> entries2 = new ArrayList<>();
        entries2.add(new Entry(0,12));
        entries2.add(new Entry(1,7));
        entries2.add(new Entry(2,8));
        entries2.add(new Entry(3,11));
        entries2.add(new Entry(4,8));
        entries2.add(new Entry(5,16));
        entries.add(new Entry(6,20));
//        LineDataSet dataSet = new LineDataSet(entries, "长沙市温度");
//        LineDataSet dataSet2 = new LineDataSet(entries2, "娄底市温度");
//        LineData lineData = new LineData(dataSet,dataSet2);
//        mLineChart.setData(lineData);
//        //设置X轴和Y轴的样式
//        setAxis();
//        setLegend();
        setDataStyle( entries,entries2);
        setAxis();
        setLegend();


    }

    @Override
    protected void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            StatusBarUtils.setStatusBarColor(AnalysisActivity.this, R.color.colorPrimaryDark);
        }
    }

    @Override
    protected void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void setDataStyle(List<Entry> entries, List<Entry> entries2) {
        LineDataSet dataSet=new LineDataSet(entries,"灌溉量/吨");
        //是否设置线的填充色
        dataSet.setDrawFilled(false);
        //折线的颜色
        dataSet.setFillColor(Color.parseColor("#ED7D31"));
        //  填充色的颜色
        dataSet.setColor(Color.parseColor("#ED7D31"));
        //  折线的宽度
        dataSet.setLineWidth(2);
        //  是否设置圆点空洞
        dataSet.setDrawCircleHole(false);
        //  设置圆点的半径
        dataSet.setCircleRadius(3);
        //  设置圆的背景颜色
        dataSet.setCircleColor(Color.parseColor("#ED7D31"));
        //  设置数值字体大小
        dataSet.setValueTextSize(13);
        //  设置数值字体颜色
        dataSet.setValueTextColor(Color.parseColor("#ED7D31"));
        //  设置折线的显示模式
        dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);

        LineDataSet dataSet2 = new LineDataSet(entries2, "覆盖面/%");
        dataSet2.setDrawFilled(false);
        dataSet2.setFillColor(Color.parseColor("#2562BE"));
        dataSet2.setColor(Color.parseColor("#2562BE"));
        dataSet2.setLineWidth(2);
        dataSet2.setDrawCircleHole(false);
        dataSet2.setCircleRadius(3);
        dataSet2.setCircleColor(Color.parseColor("#2562BE"));
        dataSet2.setValueTextSize(13);
        dataSet2.setValueTextColor(Color.parseColor("#2562BE"));
        dataSet2.setMode(LineDataSet.Mode.CUBIC_BEZIER);

        LineData lineData = new LineData(dataSet, dataSet2);

        //动画效果
        mLineChart.animateY(1500);
        mLineChart.animateX(1500);
        //设置底部间距
        mLineChart.setExtraBottomOffset(20f);


        mLineChart.setData(lineData);
        //描述信息
        Description description = mLineChart.getDescription();
        description.setText("日期");



    }


    private void setAxis() {
        //  获得X轴实例
        XAxis xl = mLineChart.getXAxis();
        //  设置X轴显示位置
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);
        //  设置X轴步长
        xl.setGranularity(1);
        //  设置是否显示X轴的延伸线
        xl.setDrawGridLines(false);

        //修改折线图X轴样式
        xl.setValueFormatter(new ValueFormatter() {
            public String getFormattedValue(float value) {
                return mLableXHeartRate[(int)value];
            }
        });
        xl.setLabelCount(6,true);

        //  获得左边Y轴实例
        YAxis yl = mLineChart.getAxisLeft();
        //  设置左边Y轴最小单位为0
        yl.setAxisMinimum(0);

        //  获得右边Y轴的实例
        YAxis yl_right = mLineChart.getAxisRight();
        //  设置右边Y轴是否可用
        yl_right.setEnabled(false);
    }

    //设置图例样式
    private void setLegend() {
        //  获得图例的实例
        Legend legend = mLineChart.getLegend();
        //  设置图例是否在图表控件内部显示
        legend.setDrawInside(false);
        //  设置图例的排列方向
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //  设置图例在图表控件中的水平对齐方向
        legend.setHorizontalAlignment
                (Legend.LegendHorizontalAlignment.CENTER);
        //  设置图例在图表控件中的垂直对齐方向
        legend.setVerticalAlignment
                (Legend.LegendVerticalAlignment.BOTTOM);
        //  设置图例的色块大小
        legend.setFormSize(15);
        //  设置图例的色块形状
        legend.setForm(Legend.LegendForm.LINE);
        //  设置图例的文字大小
        legend.setTextSize(13);
        //  设置图例的文字颜色
        legend.setTextColor(Color.BLACK);
        legend.setXEntrySpace(20f);



    }




}